package com.smyh.UIM;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.smyh.UIM.record.DeviceFaultRecordFm;
import com.smyh.UIM.record.DeviceSingleRecordFm;
import com.smyh.UIM.device.DeviceSyncOptionFm;
import com.smyh.UIM.record.DeviceSyncRecordFm;
import com.smyh.UIM.record.HostFaultRecordFm;
import com.smyh.UIM.record.HostSyncRecordFm;
import com.smyh.server.SuperAddRecordPointTread;

/**
 * Created by 姜龙 on 2016/7/6.
 */
public class RecordFragment extends Fragment {

    private View view;
    private FrameLayout footfm;

    //当前视图：碎片view
    static Fragment currentfm;
    //指示器下面：碎片view
    static Fragment hostSyncRecordFm,hostFaultRecordFm,deviceSyncRecordFm,deviceFaultRecordFm,deviceSingleRecordFm,deviceSyncOptionFm;
    static FragmentManager childFragmentManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_record, container, false);
        childFragmentManager = getFragmentManager();
        footfm = (FrameLayout) view.findViewById(R.id.footfm);

        hostSyncRecordFm = new HostSyncRecordFm();
        hostFaultRecordFm = new HostFaultRecordFm();
        deviceSyncRecordFm = new DeviceSyncRecordFm();
        deviceFaultRecordFm = new DeviceFaultRecordFm();
        deviceSingleRecordFm = new DeviceSingleRecordFm();
        deviceSyncOptionFm = new DeviceSyncOptionFm();

        currentfm = hostSyncRecordFm;
        getChildFragmentManager().beginTransaction().add(R.id.footfm,hostSyncRecordFm)
                .add(R.id.footfm,hostFaultRecordFm).add(R.id.footfm,deviceSyncRecordFm)
                .add(R.id.footfm,deviceFaultRecordFm).add(R.id.footfm,deviceSingleRecordFm)
                .add(R.id.footfm,deviceSyncOptionFm)
                .show(hostSyncRecordFm).hide(hostFaultRecordFm).hide(deviceSyncRecordFm).hide(deviceFaultRecordFm).hide(deviceSingleRecordFm).hide(deviceSyncOptionFm).commit();

        if(!SuperAddRecordPointTread.isStart){
            new SuperAddRecordPointTread().start();
            SuperAddRecordPointTread.isStart = true;
        }
        return view;
    }

    public static void setCurrentfm(int position) {
        //主机菜单
        switch (position){
            case 0:
                if(currentfm != hostSyncRecordFm){
                    currentfm = hostSyncRecordFm;
                    showCurrentFM();
                }
                break;
            case 1:
                if(currentfm != hostFaultRecordFm){
                    currentfm = hostFaultRecordFm;
                    showCurrentFM();
                }
                break;
            case 2:
                if(currentfm != deviceSyncRecordFm){
                    currentfm = deviceSyncRecordFm;
                    showCurrentFM();
                }
                break;
            case 3 :
                if(currentfm != deviceFaultRecordFm){
                    currentfm = deviceFaultRecordFm;
                    showCurrentFM();
                }
                break;
            case 4:
                if(currentfm != deviceSingleRecordFm){
                    currentfm = deviceSingleRecordFm;
                    showCurrentFM();
                }
                break;
            case 5:
                if(currentfm != deviceSyncOptionFm){
                    currentfm = deviceSyncOptionFm;
                    showCurrentFM();
                }
                break;
        }

    }

    public static void hiddenAll(){
        childFragmentManager.beginTransaction().hide(hostSyncRecordFm).hide(hostFaultRecordFm).hide(deviceSyncRecordFm)
                .hide(deviceFaultRecordFm).hide(deviceSingleRecordFm).hide(deviceSyncOptionFm).commit();
    }
    public static void showCurrentFM(){
        hiddenAll();
        childFragmentManager.beginTransaction().show(currentfm).commit();
    }


}
